home *** CD-ROM | disk | FTP | other *** search
/ PC World 2006 October / PCWorld_2006-10_cd.bin / domacnost a kancelar / winorganizer / WinOrg.exe / PluginsAndCOM / Plugins / Demo / fComment.pas < prev    next >
Pascal/Delphi Source File  |  2005-09-08  |  9KB  |  338 lines

  1. unit fComment;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, ComCtrls, ComObj, ExtCtrls, ImgList, ToolWin;
  8.  
  9. type
  10.   TfrmComment = class(TForm)
  11.     Panel1: TPanel;
  12.     Button1: TButton;
  13.     RichEdit1: TRichEdit;
  14.     ToolBar1: TToolBar;
  15.     btFirst: TToolButton;
  16.     btPrev: TToolButton;
  17.     btNext: TToolButton;
  18.     btLast: TToolButton;
  19.     ImageList1: TImageList;
  20.     PanelTitle: TPanel;
  21.     rgFormat: TRadioGroup;
  22.     btSave: TButton;
  23.     SaveDialog1: TSaveDialog;
  24.     procedure btNavigatorClick(Sender: TObject);
  25.     procedure rgFormatClick(Sender: TObject);
  26.     procedure btSaveClick(Sender: TObject);
  27.     procedure btImportHtml(Sender: TObject);
  28.   private
  29.     { Private declarations }
  30.     FTable: Variant;
  31.     FItemIndex: Integer;
  32.     FTitleIndex: Integer;
  33.     FCommentIndex: Integer;
  34.     procedure UpdateData;
  35.   public
  36.     { Public declarations }
  37.     class procedure Execute;
  38.   end;
  39.  
  40. var
  41.   frmComment: TfrmComment;
  42.  
  43. implementation
  44.  
  45. {$R *.DFM}
  46.  
  47. { TfrmComment }
  48.  
  49. class procedure TfrmComment.Execute;
  50. var
  51.   WOrg, ADoc: Variant;
  52. begin
  53.   WOrg := CreateOLEObject('WinOrganizer.App');
  54.   Application.Handle := WOrg.Handle;
  55.  
  56.   with TfrmComment.Create(Application) do
  57.   try
  58.     if WOrg.FileList.ActiveIndex < 0 then
  59.       RichEdit1.Lines.Add('No file selected')
  60.     else
  61.     begin
  62.       ADoc := WOrg.FileList.ActiveFile.Selected;
  63.       if VarIsEmpty(ADoc) then
  64.         RichEdit1.Lines.Add('No document selected')
  65.       else
  66.       if not (ADoc.isTable and WOrg.FileList.ActiveFile.TableExists(ADoc.Ext)) then
  67.         RichEdit1.Lines.Add('Selected document is not a table')
  68.       else
  69.       begin
  70.         FTable := ADoc.Open;
  71.         FItemIndex := 0;
  72.         FCommentIndex := FTable.TableDef.FieldByName('Comments').Index;
  73.         FTitleIndex := FTable.TableDef.FieldByName('Title').Index;
  74.         UpdateData;
  75.       end;
  76.     end;
  77.     ShowModal;
  78.   finally
  79.     if not (VarIsEmpty(FTable) or VarIsNull(FTable)) then
  80.     begin
  81.       FTable := NULL;
  82.       ADoc.Close;
  83.     end;
  84.     Free;
  85.   end;
  86. end;
  87.  
  88. function StreamToArrayOfByte(Stream: TMemoryStream): Variant;
  89. var
  90.   p: Pointer;
  91. begin
  92.   if Stream.Size > 0 then
  93.   begin
  94.     Result := VarArrayCreate([0, Stream.Size], varByte);
  95.     p := VarArrayLock(Result);
  96.     try
  97.       Stream.Position := 0;
  98.       Stream.Read(p^, Stream.Size);
  99.     finally
  100.       VarArrayUnlock(Result);
  101.       Stream.Position := 0;
  102.     end;
  103.   end
  104.   else
  105.     Result := NULL;
  106. end;
  107.  
  108. procedure ArrayOfByteToStream(V: Variant; Stream: TMemoryStream);
  109. var
  110.   p: Pointer;
  111. begin
  112.   if not (VarIsEmpty(V) or VarIsNull(V)) then
  113.   begin
  114.     p := VarArrayLock(V);
  115.     try
  116.       Stream.Position := 0;
  117.       Stream.Write(p^, VarArrayHighBound(V, 1));
  118.       Stream.Position := 0;
  119.     finally
  120.       VarArrayUnlock(V);
  121.     end;
  122.   end;
  123. end;
  124.  
  125. function GetTempFolder: String;
  126. var
  127.   i: Integer;
  128. begin
  129.   i := GetTempPath(0, nil);
  130.   SetLength(Result, i);
  131.   GetTempPath(i, PChar(Result));
  132.   SetLength(Result, StrLen(PChar(Result)));
  133. end;
  134.  
  135. procedure TfrmComment.UpdateData;
  136. var
  137.   s, s1: String;
  138.   ARec, RVF, V: Variant;
  139.   AStream: TMemoryStream;
  140. begin
  141.   btFirst.Enabled := (FItemIndex <> 0);
  142.   btPrev.Enabled := btFirst.Enabled;
  143.   btLast.Enabled := FItemIndex < (FTable.Count - 1);
  144.   btNext.Enabled := btLast.Enabled;
  145.  
  146.   s := '';
  147.   if FItemIndex < FTable.Count then
  148.   begin
  149.     ARec := FTable.Items[FItemIndex].Open;
  150.     try
  151.       if not ARec.isNull(FTitleIndex) then
  152.         s := ARec.Values[FTitleIndex];
  153.  
  154.       if not ARec.isNull(FCommentIndex) then
  155.       begin
  156.         V := ARec.Values[FCommentIndex];
  157.  
  158.         RVF := CreateOLEObject('WinOrganizer.RVF');
  159.         try
  160.           AStream := TMemoryStream.Create;
  161.           try
  162.             //ArrayOfByteToStream(VRTF, AStream);
  163.             case rgFormat.ItemIndex of
  164.               0: //RTF
  165.                 begin
  166.                   ArrayOfByteToStream(RVF.RVFToRTF(V), AStream);
  167.                   RichEdit1.Lines.LoadFromStream(AStream);
  168.                 end;
  169.               1: //TXT
  170.                 begin
  171.                   RichEdit1.Lines.Text := WideString(RVF.RVFToTXT(V))
  172.                 end;
  173.               2: //RVF
  174.                 begin
  175.                   ArrayOfByteToStream(V, AStream);
  176.                   RichEdit1.Lines.LoadFromStream(AStream);
  177.                 end;
  178.               3: //HTML
  179.                 begin
  180.                   s1 := GetTempFolder + 'test.html';
  181.                   RVF.RVFToHtmlFile(V, s1);
  182.                   RichEdit1.Lines.LoadFromFile(s1);
  183.                   s := s + s1;
  184.                 end;
  185.             end;
  186.  
  187.           finally
  188.             AStream.Free;
  189.           end;
  190.         finally
  191.           RVF := NULL;
  192.         end;
  193.       end
  194.       else
  195.         RichEdit1.Lines.Clear;
  196.     finally
  197.       FTable.Items[FItemIndex].Close;
  198.     end;
  199.   end;
  200.  
  201.   PanelTitle.Caption := s;
  202. end;
  203.  
  204. procedure TfrmComment.btNavigatorClick(Sender: TObject);
  205. begin
  206.   if Sender = btFirst then
  207.     FItemIndex := 0
  208.   else
  209.   if Sender = btPrev then
  210.     Dec(FItemIndex)
  211.   else
  212.   if Sender = btNext then
  213.     Inc(FItemIndex)
  214.   else
  215.   if Sender = btLast then
  216.     FItemIndex := FTable.Count - 1;
  217.  
  218.   UpdateData;
  219. end;
  220.  
  221. procedure TfrmComment.rgFormatClick(Sender: TObject);
  222. begin
  223.   RichEdit1.Lines.Clear;
  224.   RichEdit1.PlainText := (rgFormat.ItemIndex <> 0);
  225.   UpdateData;
  226. end;
  227.  
  228. procedure TfrmComment.btSaveClick(Sender: TObject);
  229. var
  230.   ARec, RVF, V: Variant;
  231.   AStream: TMemoryStream;
  232.   S: WideString;
  233. begin
  234.   if FItemIndex < FTable.Count then
  235.   begin
  236.     ARec := FTable.Items[FItemIndex].Open;
  237.     try
  238.       if not ARec.isNull(FCommentIndex) then
  239.       begin
  240.         V := ARec.Values[FCommentIndex];
  241.  
  242.         RVF := CreateOLEObject('WinOrganizer.RVF');
  243.         try
  244.           AStream := TMemoryStream.Create;
  245.           try
  246.             SaveDialog1.FileName := ChangeFileExt(SaveDialog1.FileName, '');
  247.  
  248.             case rgFormat.ItemIndex of
  249.               0: //RTF
  250.                 begin
  251.                   SaveDialog1.Filter := 'RTF files (*.rtf)|*.rtf';
  252.                   SaveDialog1.DefaultExt := '.rtf';
  253.                 end;
  254.               1: //TXT
  255.                 begin
  256.                   SaveDialog1.Filter := 'TXT files (*.txt)|*.txt';
  257.                   SaveDialog1.DefaultExt := '.txt';
  258.                 end;
  259.               2: //RVF
  260.                 begin
  261.                   SaveDialog1.Filter := 'RVF files (*.rvf)|*.rvf';
  262.                   SaveDialog1.DefaultExt := '.rvf';
  263.                 end;
  264.               3: //HTML
  265.                 begin
  266.                   SaveDialog1.Filter := 'HTML files (*.htm)|*.htm';
  267.                   SaveDialog1.DefaultExt := '.htm';
  268.                 end;
  269.             end;
  270.  
  271.             if SaveDialog1.Execute then
  272.               case rgFormat.ItemIndex of
  273.                 0: //RTF
  274.                   begin
  275.                     ArrayOfByteToStream(RVF.RVFToRTF(V), AStream);
  276.                     AStream.SaveToFile(SaveDialog1.FileName);
  277.                   end;
  278.                 1: //TXT
  279.                   begin
  280.                     S := WideString(RVF.RVFToTXT(V));
  281.                     AStream.Write(S[1], Length(S));
  282.                     AStream.SaveToFile(SaveDialog1.FileName);
  283.                   end;
  284.                 2: //RVF
  285.                   begin
  286.                     ArrayOfByteToStream(V, AStream);
  287.                     AStream.SaveToFile(SaveDialog1.FileName);
  288.                   end;
  289.                 3: //HTML
  290.                   begin
  291.                     RVF.RVFToHtmlFile(V, SaveDialog1.FileName);
  292.                   end;
  293.               end;
  294.  
  295.           finally
  296.             AStream.Free;
  297.           end;
  298.         finally
  299.           RVF := NULL;
  300.         end;
  301.       end
  302.       else
  303.         RichEdit1.Lines.Clear;
  304.     finally
  305.       FTable.Items[FItemIndex].Close;
  306.     end;
  307.   end;
  308.  
  309. end;
  310.  
  311. procedure TfrmComment.btImportHtml(Sender: TObject);
  312. var
  313.   ARec, RVF, V: Variant;
  314.   AStream: TMemoryStream;
  315.   S: WideString;
  316. begin
  317. {
  318.   if FItemIndex < FTable.Count then
  319.   begin
  320.     ARec := FTable.Items[FItemIndex].Open;
  321.     try
  322.       RVF := CreateOLEObject('WinOrganizer.RVF');
  323.       try
  324.         V := RVF.HtmlFileToRVF('c:\test.html');
  325.         ARec.Values[FCommentIndex] := V;
  326.       finally
  327.         RVF := NULL;
  328.       end;
  329.  
  330.     finally
  331.       FTable.Items[FItemIndex].Close;
  332.     end;
  333.   end;
  334. }
  335. end;
  336.  
  337. end.
  338.